package org.aeon.colorfulplanebattle.view.components;

import org.aeon.colorfulplanebattle.view.GameState;
import org.aeon.colorfulplanebattle.view.GameUI;

import javax.swing.*;
import java.awt.*;

/**
 * @author: MissingAeon
 * @date: 2024/1/10 14:35
 * @description: 启动弹出窗口
 */
public class PopUp extends JDialog {
    private final GameState gameState;

    public PopUp(GameState gameState, JPanel panel) {
        this.gameState = gameState;
        this.add(panel);
        init();
    }

    private void init() {
        // 设置关闭操作为DO_NOTHING_ON_CLOSE
        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        this.setModalityType(Dialog.DEFAULT_MODALITY_TYPE);
        this.setResizable(false); // 禁止调整大小
        this.pack();

        // 设置位置
        if (gameState.exist("gameUI") ) {
            GameUI gameUI = gameState.get("gameUI");
            int childX = gameUI.getX() + (gameUI.getWidth() - this.getWidth()) / 2;
            int childY = gameUI.getY() + (gameUI.getHeight() - this.getHeight()) / 2;
            this.setLocation(childX, childY);
        } else {
            this.setLocationRelativeTo(null);
        }
    }
}
